from ctrls cimport *

# ILvDataSetObserver
cdef void LvAdapter_DataSetObserver_notifyChanged(ILvAdapter *pObj):
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    if this.obz:
        this.obz.lpVtbl.onChanged(this.obz)

cdef void LvAdapter_DataSetObserver_notifyInvalidated(ILvAdapter *pObj):
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    if this.obz:
        this.obz.lpVtbl.onInvalidated(this.obz)

cdef void LvAdapter_DataSetObserver_notifyItemChanged(ILvAdapter *pObj,int iItem):
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    if this.obz:
        this.obz.lpVtbl.OnItemChanged(this.obz, iItem)

cdef void McAdapter_DataSetObserver_notifyChanged(IMcAdapter *pObj):
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    if this.obz:
        this.obz.lpVtbl.onChanged(this.obz)

cdef void McAdapter_DataSetObserver_notifyInvalidated(IMcAdapter *pObj):
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    if this.obz:
        this.obz.lpVtbl.onInvalidated(this.obz)

cdef void McAdapter_DataSetObserver_notifyItemChanged(IMcAdapter *pObj,int iItem):
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    if this.obz:
        this.obz.lpVtbl.OnItemChanged(this.obz, iItem)

# LvAdapter
cdef long __stdcall LvAdapter_AddRef(ILvAdapter *pObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    this.nRefs += 1
    return this.nRefs

cdef long __stdcall LvAdapter_Release(ILvAdapter *pObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    this.nRefs -= 1
    if this.nRefs == 0:
        LvAdapter_OnFinalRelease(pObj)
    return this.nRefs

cdef void __stdcall LvAdapter_OnFinalRelease(ILvAdapter *pObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    if this.obz is not NULL:
        this.obz.lpVtbl.Release(this.obz)
        this.obz = NULL
    free(this)

cdef void __stdcall LvAdapter_registerDataSetObserver(ILvAdapter *pObj, ILvDataSetObserver * observer) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    if this.obz is NULL:
        this.obz = observer
        observer.lpVtbl.AddRef(observer)

cdef void __stdcall LvAdapter_unregisterDataSetObserver(ILvAdapter *pObj, ILvDataSetObserver * observer) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    if this.obz == observer:
        this.obz = NULL
        observer.lpVtbl.Release(observer)

cdef int __stdcall LvAdapter_getCount(ILvAdapter *pObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.getCount()

cdef void __stdcall LvAdapter_getView(ILvAdapter *pObj, int position, IWindow *pItem, IXmlNode *xmlTemplate) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    self.getView(position, SWindow(<DWORD_PTR>pItem), <DWORD_PTR>xmlTemplate)

cdef int __stdcall LvAdapter_getItemViewType(ILvAdapter *pObj, int position, DWORD dwState) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.getItemViewType(position, dwState)

cdef int __stdcall LvAdapter_getViewTypeCount(ILvAdapter *pObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.getViewTypeCount()

cdef void __stdcall LvAdapter_getViewDesiredSize(ILvAdapter *pObj, SIZE *pSize, int position, IWindow *pItem, int wid, int hei) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    sz = self.GetViewDesiredSize(position, SWindow(<DWORD_PTR>pItem), wid, hei)
    pSize.cx = sz['cx']
    pSize.cy = sz['cy']

cdef BOOL __stdcall LvAdapter_isEmpty(ILvAdapter *pObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.isEmpty()

cdef void __stdcall LvAdapter_getItemDesc(ILvAdapter *pObj, int positoin, IStringW *pDesc) noexcept with gil:
    pass

cdef void __stdcall LvAdapter_InitByTemplate(ILvAdapter *pObj,IXmlNode * xmlTemplate) noexcept with gil:
    pass

cdef HRESULT __stdcall LvAdapter_QueryInterface(ILvAdapter *pObj,REFGUID id, IObjRef**ppObj) noexcept with gil:
    cdef ILvAdapterData *this = <ILvAdapterData*>(pObj - <size_t>&((<ILvAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.QueryInterface(<DWORD_PTR>id)

cdef ILvAdapterVtbl vtblLvAdapter = ILvAdapterVtbl(
    LvAdapter_AddRef,
    LvAdapter_Release,
    LvAdapter_OnFinalRelease,
    LvAdapter_registerDataSetObserver,
    LvAdapter_unregisterDataSetObserver,
    LvAdapter_getCount,
    LvAdapter_getView,
    LvAdapter_getItemViewType,
    LvAdapter_getViewTypeCount,
    LvAdapter_getViewDesiredSize,
    LvAdapter_isEmpty,
    LvAdapter_getItemDesc,
    LvAdapter_InitByTemplate,
    LvAdapter_QueryInterface
)

# McAdapter
cdef long __stdcall McAdapter_AddRef(IMcAdapter *pObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    this.nRefs += 1
    return this.nRefs

cdef long __stdcall McAdapter_Release(IMcAdapter *pObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    this.nRefs -= 1
    if this.nRefs == 0:
        McAdapter_OnFinalRelease(pObj)
    return this.nRefs

cdef void __stdcall McAdapter_OnFinalRelease(IMcAdapter *pObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    if this.obz is not NULL:
        this.obz.lpVtbl.Release(this.obz)
        this.obz = NULL
    free(this)

cdef void __stdcall McAdapter_registerDataSetObserver(IMcAdapter *pObj, ILvDataSetObserver * observer) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    if this.obz is NULL:
        this.obz = observer
        observer.lpVtbl.AddRef(observer)

cdef void __stdcall McAdapter_unregisterDataSetObserver(IMcAdapter *pObj, ILvDataSetObserver * observer) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    if this.obz == observer:
        this.obz = NULL
        observer.lpVtbl.Release(observer)

cdef int __stdcall McAdapter_getCount(IMcAdapter *pObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.getCount()

cdef void __stdcall McAdapter_getView(IMcAdapter *pObj, int position, IWindow *pItem, IXmlNode *xmlTemplate) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    self.getView(position, SWindow(<DWORD_PTR>pItem), <DWORD_PTR>xmlTemplate)

cdef int __stdcall McAdapter_getItemViewType(IMcAdapter *pObj, int position, DWORD dwState) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.getItemViewType(position, dwState)

cdef int __stdcall McAdapter_getViewTypeCount(IMcAdapter *pObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.getViewTypeCount()

cdef void __stdcall McAdapter_getViewDesiredSize(IMcAdapter *pObj, SIZE *pSize, int position, IWindow *pItem, int wid, int hei) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    sz = self.GetViewDesiredSize(position, SWindow(<DWORD_PTR>pItem), wid, hei)
    pSize.cx = sz['cx']
    pSize.cy = sz['cy']

cdef BOOL __stdcall McAdapter_isEmpty(IMcAdapter *pObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return <bool>self.isEmpty()

cdef void __stdcall McAdapter_getItemDesc(IMcAdapter *pObj, int positoin, IStringW *pDesc) noexcept with gil:
    pass

cdef void __stdcall McAdapter_InitByTemplate(IMcAdapter *pObj, IXmlNode * xmlTemplate) noexcept with gil:
    pass

cdef HRESULT __stdcall McAdapter_QueryInterface(IMcAdapter *pObj,REFGUID id, IObjRef**ppObj) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return self.QueryInterface(<DWORD_PTR>id)
    
cdef void __stdcall McAdapter_GetColumnName(const IMcAdapter* pObj, int iCol, IStringW*pName) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    s = self.GetColumnName(iCol)
    if s is not None:
        pName.lpVtbl.Assign(pName, str(s))

cdef BOOL __stdcall McAdapter_IsColumnVisible(const IMcAdapter* pObj, int iCol) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    return <bool>self.IsColumnVisible(iCol)

cdef BOOL __stdcall McAdapter_OnSort(IMcAdapter* pObj, int iCol, UINT*pFmts, int nCols) noexcept with gil:
    cdef IMcAdapterData *this = <IMcAdapterData*>(pObj - <size_t>&((<IMcAdapterData*>0).adapter))
    cdef object self = <object><PyObject*>(this.self)
    cols = []
    for n in range(nCols):
        cols.append(pFmts[n])
    return <bool>self.OnSort(iCol, cols)

cdef IMcAdapterVtbl vtblMcAdapter = IMcAdapterVtbl(
    McAdapter_AddRef,
    McAdapter_Release,
    McAdapter_OnFinalRelease,
    McAdapter_registerDataSetObserver,
    McAdapter_unregisterDataSetObserver,
    McAdapter_getCount,
    McAdapter_getView,
    McAdapter_getItemViewType,
    McAdapter_getViewTypeCount,
    McAdapter_getViewDesiredSize,
    McAdapter_isEmpty,
    McAdapter_getItemDesc,
    McAdapter_InitByTemplate,
    McAdapter_QueryInterface,
    McAdapter_GetColumnName,
    McAdapter_IsColumnVisible,
    McAdapter_OnSort
)
